/**
 * @Title: model.js
 * @Description: model
 * @author: Fanchao
 * @date: 2020/8/15 6:20 下午
 * @version V1.0
 */

import { pathToRegexp } from "path-to-regexp";
import { message } from "antd";
import { getExamList, setDefaultEvaluateExam } from "api/index";

export default {
    namespace: "Examination",
    state: {
        dataSource: [],
        keyword: ""
    },
    reducers: {
        setDataSource(state, { payload: { dataSource, keyword } }) {
            return {
                ...state,
                dataSource,
                keyword
            };
        }
    },
    effects: {
        /**
         * @desc 获取方案浏览
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getDataSource({ payload }, { call, put, select }) {
            const result = yield call(getExamList, payload);
            if (result.status === "success") {
                const dataSource = result.result;
                yield put({
                    type: "setDataSource",
                    payload: {
                        dataSource,
                        keyword: payload.keyword
                    }
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 设置默认试卷
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* setDefaultEvaluate({ payload, callback }, { call, put, select }) {
            const result = yield call(setDefaultEvaluateExam, payload);
            if (result.status === "success") {
                if (callback && typeof callback === "function") {
                    callback(true);
                }
            } else {
                message.error(result.message);
            }
        }
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                if (pathToRegexp("/evaluations/examination").exec(pathname)) {

                }
            });
        }
    }
};
